﻿using System;
using System.IO;

using Microsoft.Build.Utilities;
using Microsoft.Build.Framework;

using SharpSvn;

namespace SvnTools.Tasks.VersionManagement
{
	public class SvnGetProperty : Task
	{
		public string m_value = string.Empty;

		public string TargetPath { get; set; }

		public bool Debug { get; set; }

		[Required]
		public string PropertyName { get; set; }

		[Output]
		public string Value
		{
			get { return m_value; }
		}

		public override bool Execute()
		{
			if (Debug) { System.Diagnostics.Debugger.Break(); }

			bool success = true;

			try
			{
				// If TargetPath isn't set, use the current build directory
				string target_path = TargetPath;
				if (string.IsNullOrEmpty(target_path))
				{
					target_path = Path.GetDirectoryName(BuildEngine.ProjectFileOfTaskNode);
				}

				// Get the property value
				using (SvnClient client = new SvnClient())
				{
					success = client.GetProperty(target_path, PropertyName, out m_value);
				}
			}
			catch (Exception e)
			{
				Log.LogErrorFromException(e);
				success = false;
			}

			return success;
		}
	}
}
